package com.ruoyi.web.controller.api;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


import cn.dev33.satoken.stp.StpUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.model.BasicsBody;
import com.ruoyi.common.core.domain.model.NumBody;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.ShopContact;
import com.ruoyi.system.service.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;

import com.ruoyi.common.core.page.TableDataInfo;

@RestController
@RequestMapping("/api")
public class WebApiController extends BaseController{
    @Autowired
    private IShopBottomInfoService shopBottomInfoService;

    @Autowired
    private IShopCompanyInfoService shopCompanyInfoService;

    @Autowired
    private IShopSoftwareService shopSoftwareService;
    @Autowired
    private IShopSceneService shopSceneService;

    @Autowired
    private IShopDemandService shopDemandService;

    @Autowired
    private IShopLicenceInfoService shopLicenceInfoService;

    @Autowired
    private IShopFriendlyLinksService shopFriendlyLinksService;

    @Autowired
    private IShopOperationManualService shopOperationManualService;
    

    @Autowired
    private IShopUserService shopUserService;

    @Autowired
    private IShopVidoService shopVidoService;

    @Autowired
    private IShopContactService shopContactService;

    @Autowired
    private RedisCache redisCache;


    /**
     * 新增web用户信息
     */
    @PostMapping("/shopuser/add")
    public AjaxResult add(@RequestBody ShopUser shopUser)
    {
        return toAjax(shopUserService.insertShopUser(shopUser));
    }

    /**
     * 查询底部栏信息列表
     */
    @GetMapping("/shopbottom/list")
    public TableDataInfo list(ShopBottomInfo shopBottomInfo)
    {
        startPage();
        List<ShopBottomInfo> list = shopBottomInfoService.selectShopBottomInfoList(shopBottomInfo);
        return getDataTable(list);
    }
    /**
     * 查询企业，软件，应用场景的数量
     */
    @GetMapping("/shopbottom/getnums")
    public AjaxResult getnums()
    {
        //查询入住公司数量
        NumBody numBody=shopCompanyInfoService.getNumByAll();
        Map map = new HashMap();
        map.put("companyTotal",numBody.getCompanyTotal());
        map.put("softwareTotal",numBody.getSoftwareTotal());
        map.put("sceneTotal",numBody.getSceneTotal());
        return success(map);
    }


    /**
     * 查询公司信息列表
     */
    @GetMapping("/shopCompany/list")
    public TableDataInfo list(ShopCompanyInfo shopCompanyInfo)
    {
        startPage();
        List<ShopCompanyInfo> list = shopCompanyInfoService.selectShopCompanyInfoList(shopCompanyInfo);
        return getDataTable(list);
    }

    /**
     * 获取公司信息详细信息
     */
    @GetMapping(value = "/shopCompany/getCompanyInfo")
    public AjaxResult getInfo(Long companyId)
    {
        return success(shopCompanyInfoService.selectShopCompanyInfoByCompanyId(companyId));
    }

    /**
     * 新增公司信息
     */
    @PostMapping("/shopCompany/add")
    public AjaxResult add(@RequestBody ShopCompanyInfo shopCompanyInfo)
    {
        return toAjax(shopCompanyInfoService.insertShopCompanyInfo(shopCompanyInfo));
    }

    /**
     * 修改公司信息
     */
    @PostMapping("/shopCompany/update")
    public AjaxResult edit(@RequestBody ShopCompanyInfo shopCompanyInfo)
    {
        return toAjax(shopCompanyInfoService.updateShopCompanyInfo(shopCompanyInfo));
    }

    /**
     * 删除公司信息
     */
    @DeleteMapping("/shopCompany/remove/{companyIds}")
    public AjaxResult remove(@PathVariable Long[] companyIds)
    {
        return toAjax(shopCompanyInfoService.deleteShopCompanyInfoByCompanyIds(companyIds));
    }


    /**
     * 查询需求信息列表
     */
    @GetMapping("/shopdemand/list")
    public TableDataInfo list(ShopDemand shopDemand)
    {
        startPage();
        List<ShopDemand> list = shopDemandService.selectShopDemandList(shopDemand);
        return getDataTable(list);
    }

    /**
     * 获取需求信息详细信息
     */
    @GetMapping(value = "/shopdemand/{demandId}")
    public AjaxResult getDemandInfo(@PathVariable("demandId") Long demandId)
    {
        return success(shopDemandService.selectShopDemandByDemandId(demandId));
    }



    /**
     * 新增需求信息
     */
    @PostMapping("/shopdemand/add")
    public AjaxResult add(@RequestBody ShopDemand shopDemand)
    {
        return toAjax(shopDemandService.insertShopDemand(shopDemand));
    }







    /**
     * 查询友情连接列表
     */
    @GetMapping("/shoplinks/list")
    public AjaxResult list(ShopFriendlyLinks shopFriendlyLinks)
    {
        List<ShopFriendlyLinks> list = shopFriendlyLinksService.selectShopFriendlyLinksList(shopFriendlyLinks);
        return success(list);
    }


    /**
     * 查询友情连接列表
     */
    @GetMapping("/shoplinks/listByPage")
    public TableDataInfo listByPage(ShopFriendlyLinks shopFriendlyLinks)
    {
        startPage();
        List<ShopFriendlyLinks> list = shopFriendlyLinksService.selectShopFriendlyLinksList(shopFriendlyLinks);
        return getDataTable(list);
    }



    /**
     * 查询营业执照信息列表
     */
    @GetMapping("/shoplicence/list")
    public TableDataInfo list(ShopLicenceInfo shopLicenceInfo)
    {
        startPage();
        List<ShopLicenceInfo> list = shopLicenceInfoService.selectShopLicenceInfoList(shopLicenceInfo);
        return getDataTable(list);
    }



    /**
     * 获取营业执照信息详细信息
     */

    @GetMapping(value = "/shoplicence/{licenceId}")
    public AjaxResult getShopLicenceInfo(@PathVariable("licenceId") Long licenceId)
    {
        return success(shopLicenceInfoService.selectShopLicenceInfoByLicenceId(licenceId));
    }

    @GetMapping("/shopoperation/list")
    public AjaxResult list(ShopOperationManual shopOperationManual)
    {
        List<ShopOperationManual> list = shopOperationManualService.selectShopOperationManualList(shopOperationManual);
        return success(list);
    }

    /**
     * 查询操作指南列表
     */
    @GetMapping("/shopoperation/listByPage")
    public TableDataInfo listByPage(ShopOperationManual shopOperationManual)
    {
        startPage();
        List<ShopOperationManual> list = shopOperationManualService.selectShopOperationManualList(shopOperationManual);
        return getDataTable(list);
    }


    /**
     * 获取操作指南详细信息
     */
    @GetMapping(value = "/shopoperation/{operationId}")
    public AjaxResult getOperationManualInfo(@PathVariable("operationId") Long operationId)
    {
        return success(shopOperationManualService.selectShopOperationManualByOperationId(operationId));
    }

    /**
     * 查询应用场景信息列表
     */
    @GetMapping("/shopscene/list")
    public TableDataInfo list(ShopScene shopScene)
    {
        startPage();
        shopScene.setSearchValue(shopScene.getSceneName());
        shopScene.setSceneName(null);
        List<ShopScene> list = shopSceneService.selectShopSceneList(shopScene);
        return getDataTable(list);
    }

    /**
     * 个人中心查询应用场景信息列表
     */
    @GetMapping("/shopscene/shopSceneList")
    public TableDataInfo shopSceneList(ShopScene shopScene)
    {
        //从redis中获取用户信息
        Object loginId = StpUtil.getLoginId();
        ShopUser shopUser = (ShopUser) redisCache.getCacheObject(StpUtil.getTokenValue());
        shopScene.setCompanyId(shopUser.getCompanyId());
        //分页列表
        startPage();
        List<ShopScene> list = shopSceneService.selectShopSceneListByUserId(shopScene);
        return getDataTable(list);
    }

    /**
     * 查询应用场景信息列表
     */
    @GetMapping("/shopscene/listByPage")
    public TableDataInfo listByPage(ShopScene shopScene)
    {

        ShopUser shopUser = (ShopUser) redisCache.getCacheObject(StpUtil.getTokenValue());
        shopScene.setCompanyId(shopUser.getCompanyId());
        startPage();
        List<ShopScene> list = shopSceneService.selectShopSceneList(shopScene);
        return getDataTable(list);
    }

    /**
     * 应用场景批量新增
     */
    @PostMapping("/shopscene/addShopSceneList")
    public AjaxResult addShopSceneList(@RequestBody List<ShopScene> shopSceneList)
    {
        Object loginId = StpUtil.getLoginId();
        Long userId = Long.valueOf((String) loginId);
        ShopUser shopUser = shopUserService.selectShopUserByUserId(userId);
        for(ShopScene scene:shopSceneList){
            scene.setCompanyId(shopUser.getCompanyId());
            shopSceneService.insertShopScene(scene);
        }
        return AjaxResult.success();
    }




    /**
     * 获取应用场景信息详细信息
     */
    @GetMapping(value = "/shopscene/getSceneInfo/{sceneId}")
    public AjaxResult getSceneInfo(@PathVariable("sceneId") Long sceneId)
    {
        return success(shopSceneService.selectShopSceneBySceneId(sceneId));
    }



    /**
     * 修改应用场景信息
     */
    @PostMapping("/shopscene/update")
    public AjaxResult edit(@RequestBody ShopScene shopScene)
    {
        return toAjax(shopSceneService.updateShopScene(shopScene));
    }


    /**
     * 删除应用场景信息
     */
    @PostMapping("/shopscene/remove/{sceneIds}")
    public AjaxResult removeShopsceneById(@PathVariable Long[] sceneIds)
    {
        return toAjax(shopSceneService.deleteShopSceneBySceneIds(sceneIds));
    }





    /**
     * 个人中心查询软件池列列表
     */
    @GetMapping("/shopsoftware/list")
    public TableDataInfo list(ShopSoftware shopSoftware)
    {
        //从redis中获取用户信息
        Object loginId = StpUtil.getLoginId();
        ShopUser shopUser = (ShopUser) redisCache.getCacheObject(StpUtil.getTokenValue());
        shopSoftware.setCompanyId(shopUser.getCompanyId());
        startPage();
        List<ShopSoftware> list = shopSoftwareService.selectShopSoftwareList1(shopSoftware);
        return getDataTable(list);
    }

    /**
     * 查询软件池列列表三级查询
     */
    @GetMapping("/shopsoftware/querySoftwareList")
    public TableDataInfo querySoftwareList(ShopSoftware params)
    {
        startPage();
        List<Map> list = shopSoftwareService.querySoftwareList(params);
        return getDataTable(list);
    }


    /**
     * 新增软件池列
     */

    @PostMapping("/shopsoftware/add")
    public AjaxResult add(@RequestBody ShopSoftware shopSoftware)
    {
        ShopUser redisShopUser = (ShopUser) redisCache.getCacheObject(StpUtil.getTokenValue());
        shopSoftware.setCompanyId(redisShopUser.getCompanyId());
        return toAjax(shopSoftwareService.insertShopSoftware(shopSoftware));
    }



    @PostMapping("/shopsoftware/addSoftwareList")
    public AjaxResult addSoftwareList(@RequestBody List<ShopSoftware> SoftwareList)
    {
        for(ShopSoftware software:SoftwareList){
            ShopUser redisShopUser = (ShopUser) redisCache.getCacheObject(StpUtil.getTokenValue());
            software.setCompanyId(redisShopUser.getCompanyId());
            shopSoftwareService.insertShopSoftware(software);
        }
        return AjaxResult.success();
    }

    /**
     * 获取软件池列详细信息
     */
    @GetMapping(value = "/shopsoftware/softwareInfo/{softwareId}")
    public AjaxResult getSoftwareInfo(@PathVariable("softwareId") Long softwareId)
    {
        return success(shopSoftwareService.selectShopSoftwareBySoftwareAndCompanyId(softwareId));
    }


    /**
     * 修改软件池列
     */
    @PostMapping("/shopsoftware/update")
    public AjaxResult edit(@RequestBody ShopSoftware shopSoftware)
    {
        return toAjax(shopSoftwareService.updateShopSoftware(shopSoftware));
    }



    /**
     * 删除软件池列
     */

    @PostMapping("/shopsoftware/remove/{softwareIds}")
    public AjaxResult removeSoftwareById(@PathVariable Long[] softwareIds)
    {
        return toAjax(shopSoftwareService.deleteShopSoftwareBySoftwareIds(softwareIds));
    }





    /**
     * 账号管理信息列表
     */
    @GetMapping("/shopuser/list")
    public TableDataInfo list(ShopUser shopUser)
    {

        //从redis中获取用户信息
        ShopUser redisShopUser = (ShopUser) redisCache.getCacheObject(StpUtil.getTokenValue());
        shopUser.setCompanyId(redisShopUser.getCompanyId());

        startPage();
        List<ShopUser> list = shopUserService.selectShopUserList(shopUser);
        return getDataTable(list);
    }



    /**
     * 获取web用户信息详细信息
     */
    @GetMapping(value = "/shopuser/getUserInfo/{userId}")
    public AjaxResult getUserInfo(@PathVariable("userId") Long userId)
    {
        return success(shopUserService.selectShopUserByUserId(userId));
    }

    /**
     * 修改web用户信息
     */
    @PostMapping("/shopuser/update")
    public AjaxResult edit(@RequestBody ShopUser shopUser)
    {
        return toAjax(shopUserService.updateShopUser(shopUser));
    }


    /**
     * 删除web用户信息
     */
    @PostMapping("/shopuser/removeShopUserById/{userIds}")
    public AjaxResult removeShopUserById(@PathVariable Long[] userIds)
    {
        return toAjax(shopUserService.deleteShopUserByUserIds(userIds));
    }






    /**
     * 查询视频列表
     */
    @GetMapping("/shopvido/list")
    public TableDataInfo list(ShopVido shopVido)
    {
        startPage();
        List<ShopVido> list = shopVidoService.selectShopVidoList(shopVido);
        return getDataTable(list);
    }


    /**
     * 获取视频详细信息
     */
    @GetMapping(value = "/shopvido/{vidoId}")
    public AjaxResult getVidoInfo(@PathVariable("vidoId") Long vidoId)
    {
        return success(shopVidoService.selectShopVidoByVidoId(vidoId));
    }



    /**
     * 获取基础信息
     */

    @GetMapping(value = "getShopCompanyInfo")
    public AjaxResult getCompanyAndUserInfo()
    {
        return success(shopUserService.getShopCompanyInfoByUserId(StpUtil.getLoginId(0L)));
    }


    /**
     * 修改基础信息
     */

    @PostMapping(value = "updateCompanyAndUserInfo")
    public AjaxResult updateCompanyAndUserInfo(@RequestBody BasicsBody basicsBody)
    {
        Object loginId = StpUtil.getLoginId();
        long userId = Long.parseLong((String) loginId);
        basicsBody.setUserId(userId);
        shopUserService.updateCompanyAndUserInfo(basicsBody);
        shopLicenceInfoService.updateCompanyAndUserInfo(basicsBody);
        return success(shopCompanyInfoService.updateCompanyAndUserInfo(basicsBody));
    }




    /**
     *联系人分页列表
     */
    @GetMapping("/contact/list")
    public TableDataInfo list(ShopContact shopContact)
    {
        startPage();
        List<ShopContact> list = shopContactService.selectShopContactList(shopContact);
        return getDataTable(list);
    }

    /**
     * 获取联系人详细信息
     */

    @GetMapping(value = "/contact/getInfo/{contactId}")
    public AjaxResult getContactInfo(@PathVariable("contactId") Long contactId)
    {
        return success(shopContactService.selectShopContactByContactId(contactId));
    }

    /**
     * 新增联系人
     */
    @PostMapping("/contact/add")
    public AjaxResult add(@RequestBody ShopContact shopContact)
    {

        ShopUser shopUser = (ShopUser) redisCache.getCacheObject(StpUtil.getTokenValue());
        shopContact.setCompanyId(shopUser.getCompanyId());

        return toAjax(shopContactService.insertShopContact(shopContact));
    }

    /**
     * 修改联系人
     */
    @PostMapping("/contact/update")
    public AjaxResult edit(@RequestBody ShopContact shopContact)
    {
        return toAjax(shopContactService.updateShopContact(shopContact));
    }

    /**
     * 删除联系人
     */
    @PostMapping("/contact/removeContact/{contactIds}")
    public AjaxResult removeContact(@PathVariable Long[] contactIds)
    {
        return toAjax(shopContactService.deleteShopContactByContactIds(contactIds));
    }


//    友情链接列表
    @GetMapping("links/list")
    public TableDataInfo linksList(ShopFriendlyLinks shopFriendlyLinks)
    {

        List<ShopFriendlyLinks> list = shopFriendlyLinksService.selectShopFriendlyLinksList(shopFriendlyLinks);
        return getDataTable(list);
    }
//    操作指南列表
    @GetMapping("operation/list")
    public TableDataInfo operationList(ShopOperationManual shopOperationManual)
    {
        List<ShopOperationManual> list = shopOperationManualService.selectShopOperationManualList(shopOperationManual);
        return getDataTable(list);
    }



}
